const connect = require('../utils/db')

const find = async () => {
  const {db, client} = await connect()
  const collection = db.collection('movies')

  // 查询操作
  return collection.find({}).toArray()

  client.close()
}

const add = async (movie) => {
  const {db, client} = await connect()
  const collection = db.collection('movies')
  let result = await collection.insertOne(movie)
  client.close()
  return result
}

const patch = async (movie) => {
  const {db, client} = await connect()
  const collection = db.collection('movies')
  let result = await collection.updateOne({nm: 'aa'}, {
    $set: movie
  })
  client.close()
  return result
}

const put = async (movie) => {
  const {db, client} = await connect()
  const collection = db.collection('movies')
  await collection.findOneAndDelete({nm: '云做的翅膀'}, {
    $set: movie
  })
  let result = await collection.insertOne(movie)
  client.close()
  return result
}

module.exports = {
  find,
  add,
  patch,
  put
}